package guinong.com.gnw.ui.activty;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import butterknife.Bind;
import butterknife.OnClick;


import guinong.com.gnw.R;
import guinong.com.gnw.base.BaseActivity;
import guinong.com.gnw.mvp.Main_Presenter;
import guinong.com.gnw.mvp.Main_View;
import guinong.com.gnw.ui.fragement.HomeFragment;
import guinong.com.gnw.ui.fragement.CenterFragment;
import guinong.com.gnw.ui.fragement.ServicesFragment;
import guinong.com.gnw.ui.widged.MainTabView;


/**
 * @说 明：近来有点闲 所以需要重新重新考虑写福彩   mvp模式
 * @项目名称：
 * @创 建人： chenshuangniu
 * @创建时间： 2016/12/15 0015   13:16
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：2016年12月17日14:57:15 实现数据库数据的创建
 * @修改备注：
 * @作用：这里会首先进来实现历史数据的加载和更新 在进行数据的操作
 */
public class MainActivity extends BaseActivity<Main_Presenter> implements Main_View {
    private static final String TAG = "MainActivity";
    private final static int FRAGMENT_COUNT = 3;//多少个Fragment
    private final static int TAB_GRAP = 0;
    private final static int TAB_WAYBILL = 1;
    private final static int TAB_TOOL = 2;
    private final int contentId = R.id.main_tab_content;
    private final int textColorChecked = R.color.theme;
    private final int textColorUnChecked = R.color.defaultColor;
    @Bind(R.id.tabview_grap)
    MainTabView tabGrap;
    @Bind(R.id.tabview_waybill)
    MainTabView tabWaybill;
    @Bind(R.id.tabview_tool)
    MainTabView tabTool;
    private MainTabView[] mTabView = null;
    private HomeFragment grapFragment;
    private ServicesFragment wayBillFragment;
    private CenterFragment toolFragment;
    private int currentTab = 0;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void setTitle() {

    }

    @Override
    public void clickButtonListener(String name, View v) {

    }

    @Override
    public void getInputStringListener(String btnName, String[] inputs) {

    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        mPresenter = new Main_Presenter(this, this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mTabView = new MainTabView[FRAGMENT_COUNT];
        mTabView[TAB_GRAP] = tabGrap;
        mTabView[TAB_WAYBILL] = tabWaybill;
        mTabView[TAB_TOOL] = tabTool;
        currentTab = TAB_GRAP;
        switchFragment(currentTab);
    }

    /**
     * 选择哪个fragment
     **/
    private void switchFragment(int currentTab) {
        this.currentTab = currentTab;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        resetView();
        mTabView[currentTab].setTextColor(getResources().getColor(textColorChecked));
        String title = "";
        switch (currentTab) {
            case TAB_GRAP:
                mTabView[TAB_GRAP].setDotText("");
                mTabView[TAB_GRAP].setIconDrable(R.mipmap.ic_home);
                if (grapFragment == null) {
                    grapFragment = new HomeFragment();
                    transaction.add(contentId, grapFragment);
                } else {
                    transaction.show(grapFragment);
                }
                break;
            case TAB_WAYBILL:
                mTabView[TAB_WAYBILL].setIconDrable(R.mipmap.ic_convenience);
                if (wayBillFragment == null) {
                    wayBillFragment = new ServicesFragment();
                    transaction.add(contentId, wayBillFragment);
                } else {
                    transaction.show(wayBillFragment);
                }
                break;
            case TAB_TOOL:
                mTabView[TAB_TOOL].setIconDrable(R.mipmap.ic_mecenter);
                if (toolFragment == null) {
                    toolFragment = new CenterFragment();
                    transaction.add(contentId, toolFragment);
                } else {
                    transaction.show(toolFragment);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * view资源恢复默认状态
     **/
    private void resetView() {
        mTabView[TAB_GRAP].setIconDrable(R.drawable.tab_grap);
        mTabView[TAB_WAYBILL].setIconDrable(R.drawable.tab_waybill);
        mTabView[TAB_TOOL].setIconDrable(R.drawable.tab_tool);
        for (int i = 0; i < mTabView.length; i++) {
            mTabView[i].setTextColor(textColorUnChecked);
        }
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (grapFragment != null) {
            transaction.hide(grapFragment);
        }
        if (wayBillFragment != null) {
            transaction.hide(wayBillFragment);
        }
        if (toolFragment != null) {
            transaction.hide(toolFragment);
        }
    }

    @OnClick({R.id.tabview_grap, R.id.tabview_waybill, R.id.tabview_tool})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tabview_grap:
                switchFragment(TAB_GRAP);
                break;
            case R.id.tabview_waybill:
                switchFragment(TAB_WAYBILL);
                break;
            case R.id.tabview_tool:
                switchFragment(TAB_TOOL);
                break;
        }
    }


    @Override
    public void saveDBFail() {

    }

    @Override
    public void saveDBSuccess() {

    }

    @Override
    public void updateSuccess() {

    }
}
